package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.Tclog;
import school.mapper.TclogMapper;
import school.support.BaseController;
import school.support.pojo.TclogPojo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

@Controller
@RequestMapping("tclog")
public class TclogController extends BaseController {

    @Autowired
    private TclogMapper tclogMapper;

    @RequestMapping("list")
    public String list(String tcname,String username,String phone, @RequestParam(defaultValue = "1") Integer pageNum, Model model,
                       String starttime,String endtime){
        Example example = new Example(Tclog.class);
        Example.Criteria criteria = example.createCriteria();
        if (phone != null) {
            criteria.andCondition("u.phone like '%" + phone + "%'");
        }
        if (username != null) {
            criteria.andCondition("u.name like '%" + username + "%'");
        }
        if (tcname != null) {
            criteria.andCondition("t.name like '%" + tcname + "%'");
        }
        if (!sessionUser().getPhone().equals("admin")) {
            criteria.andCondition("u.id = ", sessionUser().getId());
        }
        if(!StringUtil.isEmpty(starttime)){
            criteria.andCondition("l.addtime >= ",starttime);
        }
        if(!StringUtil.isEmpty(endtime)){
            criteria.andCondition("l.addtime <= ",endtime);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<TclogPojo> pageInfo = new PageInfo<>(tclogMapper.selectPoByExample(example),5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "tclog/list";
    }

}
